package com.yango.teach.controller.front;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yango.commonutil.result.Result;
import com.yango.commonutil.utils.JwtUtils;
import com.yango.serviceutil.vo.MemberVo;
import com.yango.teach.client.MemberClient;
import com.yango.teach.entity.Comment;
import com.yango.teach.entity.Course;
import com.yango.teach.service.CommentService;
import com.yango.teach.service.CourseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import javassist.compiler.CompileError;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.swing.*;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 课程评论 前端控制器
 * </p>
 *
 * @author LiDingYin
 * @since 2021-08-04
 */
@Api(description = "课程评论-前台网站 前端控制器")
@RestController
@RequestMapping("/teach/front/comment")
public class CommentFrontController {

    @Autowired
    private CommentService commentService;

    @Autowired
    private MemberClient memberClient;

    @Autowired
    private CourseService courseService;

    //根据课程id分页查询评论列表
    @ApiOperation(value = "分页查询评论列表")
    @GetMapping("/pageList/{current}/{limit}/{courseId}")
    public Result pageList(@ApiParam(name = "current", value = "当前页", required = true)
                           @PathVariable long current,
                           @ApiParam(name = "limit", value = "每页显示记录数", required = true)
                           @PathVariable long limit,
                           @ApiParam(name = "courseId", value = "课程id", required = true)
                           @PathVariable String courseId) {
        //创建page对象
        Page<Comment> page = new Page<>(current, limit);
        //构建条件
        QueryWrapper<Comment> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id", courseId);
        wrapper.orderByDesc("create_time");//根据评论时间降序排序
        //调用方法查询
        commentService.page(page, wrapper);
        //封装进map集合
        Map<String, Object> map = new HashMap<>();
        map.put("items", page.getRecords());//记录集合
        map.put("pages", page.getPages());//总页数
        map.put("total", page.getTotal());//总记录数
        map.put("size", page.getSize());//每页记录数
        map.put("current", page.getCurrent());//当前页
        map.put("hasPrevious", page.hasPrevious());//是否有上一页
        map.put("hasNext", page.hasNext());//是否有下一页

        return Result.ok().data(map);
    }


    //评论课程
    @ApiOperation(value = "评论课程")
    @PostMapping("/saveComment")
    public Result saveComment(@ApiParam(name = "comment", value = "评论对象", required = true)
                              @RequestBody Comment comment,
                              HttpServletRequest request) {
        //判断用户是否登录-根据request是否有值
        if (StringUtils.isEmpty(request)) {
            return Result.error().code(28004).message("请先登录!");
        }
        //根据token获取用户id
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        //远程调用，根据用户id获取用户信息
        MemberVo user = memberClient.getUserInfo(memberId);
        //设置用户id
        comment.setMemberId(memberId);
        comment.setAvatar(user.getAvatar());//头像
        comment.setNickname(user.getNickname());//昵称
        //调用方法添加
        boolean save = commentService.save(comment);
        if (save) {
            //根据课程id查询课程信息
            Course course = courseService.getById(comment.getCourseId());
            //课程表课程评论值加一
            course.setCommentCount(course.getCommentCount() + 1);
            courseService.updateById(course);
            return Result.ok().message("评论课程成功!");
        } else {
            return Result.error().message("评论课程失败!");
        }
    }

}

